<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="deviceNo">
        <el-input
          v-model="queryParams.deviceNo"
          placeholder="请输入设备编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地震预警是否打开" prop="earthquakeOpenOrNot">
        <el-input
          v-model="queryParams.earthquakeOpenOrNot"
          placeholder="请输入地震预警是否打开"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预警中心名称" prop="centerName">
        <el-input
          v-model="queryParams.centerName"
          placeholder="请输入预警中心名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间">
        <el-date-picker
          v-model="daterangeUpdateTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['device:pressureSensorAlarmDevice:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:pressureSensorAlarmDevice:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:pressureSensorAlarmDevice:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:pressureSensorAlarmDevice:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pressureSensorAlarmDeviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数据ID" align="center" prop="id" />
      <el-table-column label="设备编号" align="center" prop="deviceNo" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="云端订阅主题" align="center" prop="sendToCloudTopic" />
      <el-table-column label="云端发布主题" align="center" prop="getFromCloudTopic" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="授时服务器" align="center" prop="ntpServer" />
      <el-table-column label="切断装置发布主题" align="center" prop="alarmDeviceSendTopic" />
      <el-table-column label="切断装置订阅主题" align="center" prop="alarmDeviceGetTopic" />
      <el-table-column label="本地震级预警阈值" align="center" prop="deviceMagnitude" />
      <el-table-column label="本地烈度预警阈值" align="center" prop="configIntensity" />
      <el-table-column label="地震预警是否打开" align="center" prop="earthquakeOpenOrNot" />
      <el-table-column label="预警中心名称" align="center" prop="centerName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:pressureSensorAlarmDevice:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:pressureSensorAlarmDevice:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改压力报警灯对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号" prop="deviceNo">
          <el-input v-model="form.deviceNo" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="云端订阅主题" prop="sendToCloudTopic">
          <el-input v-model="form.sendToCloudTopic" placeholder="请输入云端订阅主题" />
        </el-form-item>
        <el-form-item label="云端发布主题" prop="getFromCloudTopic">
          <el-input v-model="form.getFromCloudTopic" placeholder="请输入云端发布主题" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="授时服务器" prop="ntpServer">
          <el-input v-model="form.ntpServer" placeholder="请输入授时服务器" />
        </el-form-item>
        <el-form-item label="切断装置发布主题" prop="alarmDeviceSendTopic">
          <el-input v-model="form.alarmDeviceSendTopic" placeholder="请输入切断装置发布主题" />
        </el-form-item>
        <el-form-item label="切断装置订阅主题" prop="alarmDeviceGetTopic">
          <el-input v-model="form.alarmDeviceGetTopic" placeholder="请输入切断装置订阅主题" />
        </el-form-item>
        <el-form-item label="本地震级预警阈值" prop="deviceMagnitude">
          <el-input v-model="form.deviceMagnitude" placeholder="请输入本地震级预警阈值" />
        </el-form-item>
        <el-form-item label="本地烈度预警阈值" prop="configIntensity">
          <el-input v-model="form.configIntensity" placeholder="请输入本地烈度预警阈值" />
        </el-form-item>
        <el-form-item label="地震预警是否打开" prop="earthquakeOpenOrNot">
          <el-input v-model="form.earthquakeOpenOrNot" placeholder="请输入地震预警是否打开" />
        </el-form-item>
        <el-form-item label="预警中心名称" prop="centerName">
          <el-input v-model="form.centerName" placeholder="请输入预警中心名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 配置设备对话框 -->
    <el-dialog title="配置参数" :visible.sync="settingFormOpen" width="50%" append-to-body>
      <el-form ref="settingForm" :model="settingForm" label-width="80px">
        <el-descriptions :column="2" style="background: #ffffff00; color: #FFFFFF" border>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-info"></i>
              设备序列号
            </template>
            {{ settingForm.sn }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-star-on"></i>
              厂家编码
            </template>
            {{ settingForm.pkey }}
          </el-descriptions-item>
        </el-descriptions>
        <el-tabs v-loading="settingDialogLoading" v-model="activeSettingTabName" type="border-card" style="margin-top: 10px;">
          <el-tab-pane label="报警参数" name="first">
            <el-descriptions :column="2" style="background: #ffffff00; color: #FFFFFF; overflow-x: auto" border>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-upload2"></i>
                  高高限阈值
                </template>
                <el-input-number v-model="settingForm.limitAlarmHh" :min="0"></el-input-number> （kPa）
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-top"></i>
                  高限阈值
                </template>
                <el-input-number v-model="settingForm.limitAlarmH" :min="0"></el-input-number> （kPa）
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-bottom"></i>
                  低限阈值
                </template>
                <el-input-number v-model="settingForm.limitAlarmL" :min="0"></el-input-number> （kPa）
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-download"></i>
                  低低限阈值
                </template>
                <el-input-number v-model="settingForm.limitAlarmLl" :min="0"></el-input-number> （kPa）
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-sort-down"></i>
                  量程低值
                </template>
                <el-input-number v-model="settingForm.rangeL" :min="0" :step="5"></el-input-number> （kPa）
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-sort-up"></i>
                  量程高值
                </template>
                <el-input-number v-model="settingForm.rangeH" :min="0" :step="5"></el-input-number> （kPa）
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-refresh"></i>
                  数据上传周期
                </template>
                <el-input-number v-model="settingForm.dc" :min="0" :step="10"></el-input-number> （秒）
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-location"></i>
                  设备位置信息
                </template>
                <el-input placeholder="请输入地址" v-model="settingForm.address">
                  <template slot="append"><i class="el-icon-map-location"></i></template>
                </el-input>
              </el-descriptions-item>
            </el-descriptions>
          </el-tab-pane>
          <el-tab-pane label="设备管理" name="second">
            <el-descriptions :column="2" style="background: #ffffff00; color: #FFFFFF" border>
              <el-descriptions-item>
                <template slot="label">
                  <i :class="settingForm.factoryId ? 'el-icon-unlock' : 'el-icon-lock'"></i>
                  设备厂内安装身份ID
                </template>
                <el-input placeholder="请输入设备厂内安装身份ID" v-model="settingForm.factoryId"></el-input>
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-timer"></i>
                  低压报警
                </template>
                <el-switch v-model="settingForm.lowAlarm" active-text="开启" inactive-text="关闭"></el-switch>
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i :class="settingForm.solenoidValveOnOff ? 'el-icon-unlock' : 'el-icon-lock'"></i>
                  电磁阀通断控制
                </template>
                <el-switch v-model="settingForm.solenoidValveOnOff" active-text="开启" inactive-text="关闭"></el-switch>
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-timer"></i>
                  设备校时
                </template>
                <el-link type="primary" @click="ntpHandler">校时<i class="el-icon-refresh el-icon--right"></i> </el-link>
              </el-descriptions-item>
            </el-descriptions>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="settingFormSetting" type="primary" @click="submitSettingForm">确 定</el-button>
        <el-button @click="settingFormOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPressureSensorAlarmDevice, getPressureSensorAlarmDevice, delPressureSensorAlarmDevice, addPressureSensorAlarmDevice, updatePressureSensorAlarmDevice } from "@/api/device/pressureSensorAlarmDevice";

export default {
  name: "PressureSensorAlarmDevice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 压力报警灯表格数据
      pressureSensorAlarmDeviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeCreateTime: [],
      // 备注时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceNo: null,
        deviceName: null,
        earthquakeOpenOrNot: null,
        centerName: null,
        createTime: null,
        updateTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      activeSettingTabName: 'first',
      settingFormOpen: false,
      settingFormSetting: false,
      settingDialogLoading: false,
      settingForm: {
        limitAlarmHh: 100,
        limitAlarmH: 90,
        limitAlarmL: 10,
        limitAlarmLl: 5,
        rangeL: 0,
        rangeH: 2500,
        dc: 10,
        ntpOrNot: 0,
        address: '-',
        solenoidValveOnOff: true,
        pkey: null,
        sn: null,
        factoryId: null,
        lowAlarm: false
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询压力报警灯列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] = this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      listPressureSensorAlarmDevice(this.queryParams).then(response => {
        this.pressureSensorAlarmDeviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceNo: null,
        deviceName: null,
        sendToCloudTopic: null,
        getFromCloudTopic: null,
        longitude: null,
        latitude: null,
        ntpServer: null,
        alarmDeviceSendTopic: null,
        alarmDeviceGetTopic: null,
        deviceMagnitude: null,
        configIntensity: null,
        earthquakeOpenOrNot: null,
        centerName: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加压力报警灯";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPressureSensorAlarmDevice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改压力报警灯";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePressureSensorAlarmDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPressureSensorAlarmDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除压力报警灯编号为"' + ids + '"的数据项？').then(function() {
        return delPressureSensorAlarmDevice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/pressureSensorAlarmDevice/export', {
        ...this.queryParams
      }, `pressureSensorAlarmDevice_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
